# 输入：读取HOTLINENUM12的三个列(内容分类,信访概况,难度),文件位置"../data/芜湖热线12月类别统计.xls"
# 输出：由于NER模型对长句子未训练，所以输出到Excel中手动完善(信访概况)，文件位置"./Excel_to_be_optimized.xls"
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(内容分类,实体事件)进行手动升序排序,再执行To_Library文件,否则难度规则库会错误。
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(内容分类,实体事件)进行手动升序排序,再执行To_Library文件,否则难度规则库会错误。
import json
import pandas as pd
import requests
import xlrd
import xlwt
from config import post_url, doc_data_dir, HOTLINE_12, \
    HOTLINE_INCIDENT_DIFFICULTY, data_labeled, POLICE, data_incident, KEY_PROJECT_CONSTRUCTION_MANAGEMENT_DIVISION, \
    KEY_PROJECT_CONSTRUCTION_MANAGEMENT_DIVISION_INCIDENT


class Post:

    def get_data(self):
        classification_list = []  # 内容分类列表
        content_list = []  # 内容列表 此为NER模型的输入
        difficulty_list = []  # 难度列表
        workbook = xlrd.open_workbook(INPUT_FILENAME)
        table1 = workbook.sheets()[SHEET]
        for i in range(1, table1.nrows):
            # 内容分类
            str1 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_LEVEL_1)
            str2 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_LEVEL_2)
            str3 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_LEVEL_3)
            Content_classification = str1+"_"+str2+"_"+str3
            # Content_classification = table1.cell_value(i, 11)  # 内容分类
            classification_list.append(Content_classification)
            # 热线标题列表
            content = table1.cell_value(i, HOTLINE_TITLE_COL)  # 热线标题
            content_list.append(content)
            # 难度值列表
            difficulty = table1.cell_value(i, DIFFICULTY_COL)  # 难度
            difficulty_list.append(difficulty)
        print("content_list的长度：", len(content_list))
        return classification_list, content_list, difficulty_list

    def post(self):
        incident_list = []  # 实体事件列表
        # post_url = "http://36.7.159.235:10015/role"
        classification_list, content_list, difficulty_list = Post().get_data()
        # 调用NER模型接口
        for i in content_list:
            count = 0
            request_param = {"string": i}
            # post请求
            response = requests.post(self, request_param)
            load_data = json.loads(response.text)  # 将响应内容转换为Json对象
            # print("当前是第", content_list.index(i) + 1, "个信访概况", "分段后的实体个数：", len(load_data['text']), end=", ")
            # 如果没有返回的实体，则输出“无返回的text”
            if len(load_data['text']) == 0:
                # print("无返回的text", end=", ")
                incident_list.append("无返回的TEXT")
                # incident_list.append(i)
                # continue
            # 如果有返回的实体，则搜索是否有INCIDENT
            else:
                for index in range(len(load_data['text'])):
                    if load_data['text'][index]['type'] == "VILLEGA":
                        count = count + 1
                        # print("count", count, end=", ")
                        # print("INCIDENT:", load_data['text'][index]['word'], end=" ")
                        incident_list.append(load_data['text'][index]['word'])
                        break
                if count == 0:
                    # print("count", count, end=", ")
                    # print("有text,无incdent", end=" ")
                    incident_list.append("有TEXT,无ADDRESS")
                    # incident_list.append(i)
                else:
                    # print("count", count, end=", ")
                    count = 0
            # print("难度：", difficulty_list[content_list.index(i)])
        print(len(incident_list), " ", len(difficulty_list))
        test = pd.DataFrame({'内容分类': classification_list, 'VILLEGA': incident_list, '广度': difficulty_list})
        test.to_excel(OUTPUT_FILENAME, encoding="utf-8")


if __name__ == '__main__':
    # 几个参数
    # INPUT_FILENAME（输入的EXCEL文件名）
    INPUT_FILENAME = data_labeled + POLICE
    # OUTPUT_FILENAME(输出规则库的文件名)
    OUTPUT_FILENAME = data_incident + 'police_village.xls'
    # 第几个sheet
    SHEET = 0
    # 内容分类级别的列数
    CONTENT_CLASSIFICATION_COL_LEVEL_1 = 4
    CONTENT_CLASSIFICATION_COL_LEVEL_2 = 5
    CONTENT_CLASSIFICATION_COL_LEVEL_3 = 6
    # 热线标题的列数
    HOTLINE_TITLE_COL = 7
    # 广度值的列数
    DIFFICULTY_COL = 20
    # post_url 在config文件中
    post_url = post_url
    Post.post(post_url)
